
import os, os.path, sys
import traceback as tb
from log_object import *

class GraphManager: 
	def __init__(self, parent, window):
		self.parent = parent
		self.window = window
		self.current_graphs = []
		self.user_no = 0
		self.graph_no = 0
		self.graph_name = ""
		self.param_name = ""

		self.__plugins = {}  # key=type, value=plugin
		self.__loadAllPlugins()

	def destory(self):
		for graph in self.current_graphs :
			del graph
		del self.current_graphs
		self.current_graphs = []


	def __loadAllPlugins(self):
		loaded = []
		for entry in os.listdir("graphs"):
			name, ext = os.path.splitext(entry)
			if (ext==".py" or ext==".pyc") and not name.startswith("__") and name not in loaded:
				loaded.append(name)
				self.__loadGraphPlugin( "graphs", name)
		del loaded


	def __loadGraphPlugin(self, dir, type):
		try:
			graphPlugin = __import__(dir+"."+type, globals(), locals(), [type])
			self.__plugins[type] = graphPlugin
                	print "graph plugin loaded: ", type
			return True
		except:
			print "\n==========> Error importing module: ", type
			print "".join(tb.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]))
			return False

	def setCurrentGraph(self, type, param1): 
		isRegistered = False
		for graph in self.current_graphs : 
			if graph.type == type  and graph.param1 == param1 : 
				isRegistered = True 
				break
		if isRegistered == False:
			self.graph_name = "busy_graph"
			if type == HEATMAP : 
				self.graph_name = "heatmap_graph"
			elif type == DISTANCEGRAPH : 
				self.graph_name = "distance_graph"
			elif type == VECTORGRAPH : 
				self.graph_name = "vector_graph"
			elif type == TRENDGRAPH : 
				self.graph_name = "trend_graph"
			elif type == WINDOWGRAPH : 
				self.graph_name = "window_graph"
			elif type == SPACEGRAPH : 
				self.graph_name = "space_graph"
			elif type == TESTGRAPH : 
				self.graph_name = "test_graph"

			if param1 == LOG_POINTER_MOVE :
				self.param_name = "pointer"
			elif param1 == LOG_WINDOW_CHANGE :
				self.param_name = "window"
			elif param1 == LOG_WINDOW_SIZE :
				self.param_name = "size"
			elif param1 == LOG_WINDOW_DIS_SIZE :
				self.param_name = "distsize"
			elif param1 == LOG_WINDOW_LIFETIME :
				self.param_name = "lifetime"
			elif param1 == LOG_WINDOW_DIS_TIME :
				self.param_name = "distlifetime"
			elif param1 == LOG_SECTION :
				self.param_name = "section"
			elif param1 == LOG_SECTION_DATAIN :
				self.param_name = "datain"

  			current = self.__plugins[self.graph_name].makeNew(self.parent, self.window, param1)
			self.current_graphs.append(current)
			self.graph_no = len(self.current_graphs) 

	def clearCurrentGraphList(self): 
		for graph in self.current_graphs :
			del graph
		del self.current_graphs
		self.current_graphs = []
		self.graph_no = 0

	def setNoOfUsers(self, value):
		self.user_no = value

	def draw(self, dc, start, end): 
		column = self.graph_no 
		row = 1 
		if self.graph_no == 1 or self.user_no == 1 :
			column = 1 
			row = self.graph_no 
		x =0 
		y =0 
		for graph in self.current_graphs: 
			graph.draw(dc, start, end, (x, y), (column, row))
			if column == 1 : 
				y += 1
			else: 
				x += 1
